from flask import Blueprint, Response, request
from application.logic.PostLogic import PostLogic
from application.util.ResponseUtil import ResponseUtil
from application.middleware.LoginMiddleware import login_required

# 帖子蓝图
post: Blueprint = Blueprint("post", __name__)


@post.get("/post_info_list")
def post_info_list() -> Response:
    """
    获取所有帖子信息
    """
    page: int = int(request.args.get("page", 1))
    page_size: int = int(request.args.get("page_size", 10))
    return ResponseUtil(data=PostLogic.info_list(page=page, page_size=page_size)).success()


@post.post("/add")
@login_required
def add(token: str) -> Response:
    """
    新增帖子
    :param token: Token
    """
    request_data: dict = request.get_json()
    PostLogic.add(data=request_data, token=token)
    return ResponseUtil().success()


@post.post("/delete")
@login_required
def delete(token: str) -> Response:
    """
    删除帖子
    :param token: Token
    """
    PostLogic.delete(post_id=request.args.get("id"))
    return ResponseUtil().success()


@post.post("/update")
@login_required
def update(token: str) -> Response:
    """
    更新帖子
    :param token: Token
    """
    request_data: dict = request.get_json()
    PostLogic.update(post_dict=request_data, token=token)
    return ResponseUtil().success()
